/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.form;

import org.nuxeo.gwt.habyt.invocation.client.Notification;
import org.nuxeo.gwt.habyt.invocation.client.ObjectRef;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

/**
 * May serve as a base for custom handlers. This handler is appending a red div
 * for errors and for submit and reset it does nothing special (i.e. shows an
 * alert dialog).
 * <p>
 * This is not a abstract class to be able to use as the default handler if not
 * other handler is set - it is usefull for debugging.
 * <p>
 * To create a custom handler based on this one override submit() and reset()
 * methods.
 * 
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class DefaultFormHandler implements FormHandler {

    public void reset(Form form) {
        // do nothing
    }

    public void submit(final Form form) {
        // Window.alert("Submit: " + form.getProperties());
        form.getAction().execute((Widget) form, form.getProperties());
        // action.execute();
        // if (Actions.VIEW.equals(form.getInvocation().getAction())) {
        // // TODO
        // form.getInvocation().setOutcome(form.getInvocation().getTarget());
        // Outcome.show((Widget) form, form.getInvocation());
        // EventBus.get().fireOpen(form.getInvocation().getTarget());
        // } else {
        // Invoker.invoke(form.getInvocation(), new InvocationCallback() {
        // public void onSuccess(InvocationResponse result) {
        // onSubmitSucess(result);
        // }
        //
        // public void onFailure(Throwable caught) {
        // onSubmitError(caught);
        // }
        // });
        // }
    }

    protected void onSubmitSucess(Notification response) {
        ObjectRef outcome = (ObjectRef) response.getContent();
        if (outcome != null) {
            throw new UnsupportedOperationException(
                    "Form execution outcome not yet implemented");
            // TODO load outcome
        }
    }

    protected void onSubmitError(Throwable caught) {
        Window.alert("Submit error: " + caught);
    }

    public void showError(FormControl control, String error) {
        Element el = control.getElement();
        Element div = DOM.createSpan();
        div.setInnerHTML(error);
        DOM.setStyleAttribute((com.google.gwt.user.client.Element) div,
                "color", "red");
        el.getParentElement().insertAfter(div, el);
        control.getElement().addClassName("validation-error");
        control.setError(div);
    }

    public void clearError(FormControl control) {
        Element div = (Element) control.getError();
        if (div != null) {
            div.removeFromParent();
            control.getElement().removeClassName("validation-error");
            control.setError(null);
        }
    }

}
